package elitech.vietnam.myfashion.fragment;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.MyApplication;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Member;
import elitech.vietnam.myfashion.entites.Order;
import elitech.vietnam.myfashion.entites.Post;
import elitech.vietnam.myfashion.extend.ViewPagerAdapter;
import elitech.vietnam.myfashion.view.StylerDetailView;

public class StylerFragment extends Fragment {
	public static final int LOGINACTIVITY = 1;
	private MainActivity mContext;
	private StylerDetailView mStylerDetail;
	private Post mPost = null;
	private int mPostID = 0;
	private boolean mIsMyStyle = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
		if (savedInstanceState != null) {
			mPostID = savedInstanceState.getInt("Id");
			mIsMyStyle = savedInstanceState.getBoolean("isMyStyle");
			mPostID = (mPostID==0)? -1: mPostID;
		}
		mContext = (MainActivity) getActivity();
	}

	public void setPost(Post post, boolean isMyStyle) {
		mPost = post;
		mPostID = mPost.Id;
		mIsMyStyle = isMyStyle;
	}

	public void setPostID(int postID, boolean isMyStyle) {
		mPostID = postID;
		mIsMyStyle = isMyStyle;
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("Id", mPostID);
		outState.putBoolean("isMyStyle", mIsMyStyle);	
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (mPost == null && mPostID == 0)
			throw new IllegalArgumentException("Must call setPost or setPostID first");
		if (mPost == null)
			mStylerDetail = new StylerDetailView(mContext, this, mPostID, mIsMyStyle);
		else
			mStylerDetail = new StylerDetailView(mContext, this, mPost, mIsMyStyle);
		return mStylerDetail;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == LOGINACTIVITY) {

			if (resultCode == -1) {
				final int result = data.getIntExtra("Id", -1);
				if (result != -1) {
					new CallServiceTask() {
						protected void onPreExecute() {
							mContext.mLoadingDialog.show();
						};

						private Member rs = null;

						public void work() {
							try {
								rs = service.getInfoMemberByID(result, -1);
								// rs.GCMID = GoogleCloudMessaging.getInstance(
								// mContext).register(
								// MainActivity.GCM_SENDER_ID);
								// service.updateGCMID(rs.Id, rs.GCMID);
							} catch (IOException e) {} catch (XmlPullParserException e) {}
						};

						protected void onPostExecute(Void result) {
							mStylerDetail.RefreshLayout();
							mContext.mLoadingDialog.dismiss();
							mContext.mUser = rs;
							mContext.mOrder = new Order();
							mContext.mMainPageView.mPageAdapter = new ViewPagerAdapter(mContext);
							// if (mContext.mFragmentManager
							// .getBackStackEntryCount() > 0)
							// mContext.mLastTitle =
							// getString(R.string.bestoftoday);
							// else
							// mContext.changeTitle(
							// getString(R.string.bestoftoday), false);

							mContext.mMainPageView.mViewPager.setAdapter(mContext.mMainPageView.mPageAdapter);
							if (mContext.mUser != null) {
								mContext.AddPreference("User", mContext.ConvertToJson(mContext.mUser));
								if (mContext.GetPreferenceBool("GetNotification"))
									((MyApplication) mContext.getApplicationContext()).startGCMNotification();
							} else {
								mContext.AddPreference("User", "");
							}
						};
					}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
				}
			}
		}
	}

}
